package cn.edu.sgu.www.mhxysy.controller.repository;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.repository.RoleRepositoryDTO;
import cn.edu.sgu.www.mhxysy.entity.repository.RoleRepository;
import cn.edu.sgu.www.mhxysy.pager.repository.RoleRepositoryPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.repository.RoleRepositoryService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "角色仓库管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/role_repository", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class RoleRepositoryController {

    private final RoleRepositoryService roleRepositoryService;

    @Autowired
    public RoleRepositoryController(RoleRepositoryService roleRepositoryService) {
        this.roleRepositoryService = roleRepositoryService;
    }

    @ApiOperation("初始化角色的仓库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true),
            @ApiImplicitParam(name = "count", value = "仓库数量", required = true)
    })
    @RequestMapping(value = "/init", method = RequestMethod.POST)
    public JsonResult<Void> init(
            @NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId,
            @NotNull(message = CustomMessages.NOT_NULL) Integer count) {
        roleRepositoryService.init(roleId, count);

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @ApiOperation("解锁角色仓库")
    @RequestMapping(value = "/unlock", method = RequestMethod.POST)
    public JsonResult<Void> unlock(
            @ApiParam(name = "roleId", value = "角色ID", required = true)
            @NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId) {
        roleRepositoryService.unlock(roleId);

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @ApiOperation("添加角色仓库")
    @ApiOperationSupport(ignoreParameters = "id")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated RoleRepositoryDTO repositoryDTO) {
        roleRepositoryService.insert(repositoryDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID删除角色仓库")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public JsonResult<Void> deleteById(
            @ApiParam(name = "id", value = "仓库ID", required = true)
            @NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
        roleRepositoryService.deleteById(id);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过ID修改角色仓库")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@Validated RoleRepositoryDTO repositoryDTO) {
        roleRepositoryService.updateById(repositoryDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过ID修改仓库名字")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "仓库ID", required = true),
            @ApiImplicitParam(name = "name", value = "仓库名字", required = true)
    })
    @RequestMapping(value = "/updateNameById", method = RequestMethod.POST)
    public JsonResult<Void> updateNameById(
            @NotBlank(message = "仓库" + CustomMessages.ID_NOT_NULL) String id,
            @NotBlank(message = "仓库名字" + CustomMessages.NOT_NULL) String name) {
        roleRepositoryService.updateNameById(id, name);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("查询角色的仓库列表")
    @RequestMapping(value = "/selectByRoleId", method = RequestMethod.GET)
    public JsonResult<List<RoleRepository>> selectByRoleId(
            @ApiParam(name = "roleId", value = "角色ID", required = false)
            @RequestParam(required = false) String roleId) {
        List<RoleRepository> list = roleRepositoryService.selectByRoleId(roleId);

        return JsonResult.success(null, list);
    }

    @ApiOperation("分页查询角色仓库列表")
    @RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
    public JsonResult<JsonPage<RoleRepository>> selectByPage(RoleRepositoryPager pager) {
        Page<RoleRepository> page = roleRepositoryService.selectByPage(pager);

        return JsonResult.restPage(page);
    }

}